﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.StockModule;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using System.Web.Mvc;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Core.Extension;
using AutoMapper;
using Huirui.Cavan.Domain.MainBoundedContext.ReportModule;
using Huirui.Cavan.Presentation.ViewModel.PurchaseModule;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Newtonsoft.Json;
using Huirui.Cavan.Application.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Presentation.ViewModel.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;
using Huirui.Cavan.Application.MainBoundedContext.SalesModule;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;

namespace Huirui.Cavan.Presentation.Controller.StockModule
{
    public class StockTypeController : EntityController<StockType, int, StockTypeSearch, IStockTypeViewModel, IStockTypeAppService>, IStockTypeController
    {
        public override string ControllerName
        {
            get { return ControllerConst.STOCKTYPE; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public StockTypeController()
        {
            this.Model = ModelFactory.CreateModel<IStockTypeViewModel>();
        }

        [ActionName(ActionConst.LIST)]
        [HttpPost]
        public ActionResult GetStockType()
        {
            StockTypeJsonViewModel json = new StockTypeJsonViewModel();
            json.rows = new List<StockTypeJson>();
            IList<StockType> StockTypes = this.AppService.GetBySearch(SearchFactory.CreateStockTypeSearch()).ToList();
            foreach (StockType s in StockTypes)
            {
                StockTypeJson sj = Mapper.Map<StockType, StockTypeJson>(s);
                json.rows.Add(sj);
            }
            json.total = StockTypes.Count;
            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpPost]
        public ActionResult SearchStockType(string stocktypename)
        {
            StockTypeJsonViewModel json = new StockTypeJsonViewModel();
            json.rows = new List<StockTypeJson>();
            IList<StockType> StockTypes = this.AppService.GetBySearch(SearchFactory.CreateStockTypeSearch(stocktypename)).ToList();
            foreach (StockType s in StockTypes)
            {
                StockTypeJson sj = Mapper.Map<StockType, StockTypeJson>(s);
                json.rows.Add(sj);
            }
            json.total = StockTypes.Count;
            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        public ActionResult AddStockType()
        {
            this.Model.Purpose = RouteData.Values["purpose"].ToString();
            this.Model.CmbName = RouteData.Values["cmbname"].ToString();
            return PartialView("~/Views/Stock/v1/partial/addStockTypePartial.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        public ActionResult AddStockType(StockTypeViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.ModifyDate = DateTime.Now;
            model.Item.CreateDate = DateTime.Now;
            bool isExist = (this.AppService.GetBySearch(SearchFactory.CreateStockTypeSearch(model.Item.Name)).Count() > 0);
            BoolGenericResult<StockType> result = null; 
            if (!isExist)
            {
                result = this.AppService.AddOrUpdate(model.Item);
                result.Messages.Add(result.Object.Id.ToString());
                result.Messages.Add(result.Object.Name.ToString());
            }
            else
            {
                result = new BoolGenericResult<StockType>();
                result.IsSuccess = false;
                result.Messages = new List<string> {string.Format("{0}已经存在",model.Item.Name)};
            }
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);
            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.DELETE)]
        [HttpPost]
        public JsonResult DeleteStockType(string selected)
        {
            BoolGenericResult<StockType> result = null;
            List<StockTypeJson> suptypes = JsonConvert.DeserializeObject<List<StockTypeJson>>(selected);
            foreach (StockTypeJson json in suptypes)
            {
                StockType ai = this.AppService.GetById(int.Parse(json.Id));
                result = this.AppService.Delete(ai);
                result.Messages.Add(result.Object.Id.ToString());
                result.Messages.Add(result.Object.Name.ToString());
            }
            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);
            return Json(jsonResult, "application/json");
        }
    }
}
